﻿using System;
using System.Web.UI.WebControls;
using PowerToolsWeb.DataAccess;
using PowerToolsWeb.Facade;

namespace PowerToolsWeb.Admin
{
    public partial class MessageManage : AdminPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            WdatePicker4_6 wd = new WdatePicker4_6(Page);
            wd.Set(txbStratDate, txbEndDate);
            if (!IsPostBack)
            {
                txbStratDate.Text = DateTime.Now.AddDays(-7).ToShortDateString();
                txbEndDate.Text = DateTime.Now.ToShortDateString();
                BindMsg();
            }
        }
        void BindMsg()
        {
            if (ddlRead.SelectedIndex < 0)
                return;
            DateTime dtstrat, dtend;
            if (!DateTime.TryParse(txbStratDate.Text, out dtstrat))
                return;
            if (!DateTime.TryParse(txbEndDate.Text, out dtend))
                return;
            Tpt_MessageCollection mesg = new Tpt_MessageCollection();
            mesg.ListMsg(int.Parse(ddlRead.SelectedValue), dtstrat, dtend);
            this.GridView1.DataSource = mesg.DataTable;
            this.GridView1.DataBind();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            BindMsg();
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string commandName = e.CommandName;
            switch (commandName)
            {
                case "SetRead":
                    Tpt_Message msg = new Tpt_Message();
                    if (!msg.SetReaded(int.Parse(e.CommandArgument.ToString())))
                    {
                        Alert(Page, "设置失败！");
                        return;
                    }
                    BindMsg();
                    break;
                case "DeleteMsg":
                    Tpt_Message msgdelete = new Tpt_Message();
                    if (!msgdelete.Delete(int.Parse(e.CommandArgument.ToString())))
                    {
                        Alert(Page, "删除失败！");
                        return;
                    }
                    BindMsg();
                    break;
            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex > -1)
            {
                //当鼠标停留时更改背景色
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#E8E6EA'");
                //当鼠标移开时还原背景色
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            }
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView1.PageIndex = e.NewPageIndex;
            BindMsg();
        }

    }
}
